package com.ruibang.glass.equipment.controller;

import com.ruibang.glass.equipment.constant.Constants;
import com.ruibang.glass.equipment.domain.req.device.DeviceAllPageReq;
import com.ruibang.glass.equipment.entity.device.PointCheckPlan;
import com.ruibang.glass.equipment.entity.device.ScrapReport;
import com.ruibang.glass.equipment.service.PointCheckPlanService;
import com.teaming.cloud.framework2.common.model.ResultBody;
import io.seata.common.util.CollectionUtils;
import io.seata.common.util.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.io.InputStream;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
 * <p>
 *点检计划
 * </p>
 *
 * @author mameng
 * @since 2024-03-20
 */
@RestController
@RequestMapping("/api/equipment/point/check/plan")
@Api(value = "点检计划")
public class PointCheckPlanController {
    @Autowired
    private PointCheckPlanService pointCheckPlanService;

    @PostMapping("list")
    @ApiOperation(value = "获取点检计划列表", response = ResultBody.class)
    public ResultBody getBoilerList(@RequestBody DeviceAllPageReq deviceAllPageReq) {
        return ResultBody.success(pointCheckPlanService.getCheckPlanList(deviceAllPageReq));
    }

    @PostMapping("addOrUpdateCheck")
    @ApiOperation(value = "更新编辑", response = ResultBody.class)
    public ResultBody addChild(@RequestBody PointCheckPlan pointCheckPlan) {
        return ResultBody.success(pointCheckPlanService.addOrUpdateCheck(pointCheckPlan));
    }

    @GetMapping("getInfoById/{id}")
    @ApiOperation(value = "添加子层级", response = ResultBody.class)
    public ResultBody getInfoById(@PathVariable("id") String id) {
        return ResultBody.success(pointCheckPlanService.getById(id));
    }


    //
    @PostMapping("deleteAll")
    @ApiOperation(value = "添加子层级", response = ResultBody.class)
    public ResultBody deleteAll(@RequestBody PointCheckPlan pointCheckPlan) {
        if (!CollectionUtils.isEmpty(pointCheckPlan.getIds())) {
            pointCheckPlanService.lambdaUpdate().set(PointCheckPlan::getIsDelete, Constants.ONE_STR).in(PointCheckPlan::getId, pointCheckPlan.getIds()).update();
        }
        if (!StringUtils.isEmpty(pointCheckPlan.getId())) {
            pointCheckPlanService.lambdaUpdate().set(PointCheckPlan::getIsDelete, Constants.ONE_STR).eq(PointCheckPlan::getId, pointCheckPlan.getId()).update();
        }
        return ResultBody.success();
    }

    @GetMapping("listDeviceName")
    public ResultBody getListDeviceName() {
        return pointCheckPlanService.getListDeviceName();
    }

    @GetMapping("getUserInfo")
    public ResultBody getUserInfo() {
        return pointCheckPlanService.getUserInfo();
    }


    @GetMapping("pointByDay")
    public void getCheckPlanList() {
        pointCheckPlanService.checkByDayPoint();
    }

    @GetMapping("pointByWeek")
    public void checkByWeekPoint() {
        pointCheckPlanService.checkByWeekPoint();
    }

    @GetMapping("pointByMonth")
    public void checkByMonthPoint() {
        pointCheckPlanService.checkByMonthPoint();
    }

    @GetMapping("pointByYear")
    public void pointByYear() {
        pointCheckPlanService.checkByYearPoint();
    }


    // 导入
    @PostMapping("importExcel")
    @ApiOperation(value = "导入", response = ResultBody.class)
    public ResultBody importExcel(@RequestPart("files") MultipartFile file, HttpServletRequest request) {
        InputStream inputStream = null;
        try {
            inputStream = file.getInputStream();
        } catch (IOException e) {
            e.printStackTrace();
        }
        String contentType = file.getContentType();
        System.out.println("文件类型是===========" + contentType);
        String fileName = file.getOriginalFilename();
        System.out.println("文件名字是===========" + fileName);
        try {
//            if (!fileName.endsWith(".xlsx")) {
//                return ResultBody.fail(300, "文件格式不正确！请使用.Xlsx格式");
//            }
            if (file.isEmpty()) {
                return ResultBody.fail(310, "文件为空！");
            }
            //根据路径获取这个操作excel的实例
            XSSFWorkbook wb = new XSSFWorkbook(inputStream);
            //根据页面index 获取sheet页
            Sheet sheet = wb.getSheetAt(0);
            //获取sheet页共有多少行
            int totalRos = sheet.getPhysicalNumberOfRows();
            System.out.println("=========总行数为：" + totalRos);
            if (totalRos < 2) {
                return ResultBody.fail(300, "文件禁止为只有表头！");
            }
            for (int i = 0; i < totalRos; i++) {
                if (!sheet.getRow(i).getCell(0).toString().equals("序号") && !sheet.getRow(i).getCell(0).toString().equals("")) {
                    PointCheckPlan instrumentRegister = new PointCheckPlan();
                    instrumentRegister.setPointPart(sheet.getRow(i).getCell(2).toString());
                    instrumentRegister.setPointContent(sheet.getRow(i).getCell(3).toString());
                    String number = getNumber(sheet.getRow(i).getCell(4).toString(), 1);
                    String eNumber = getNumber(sheet.getRow(i).getCell(4).toString(), 2);
                    instrumentRegister.setCycle(number);
                    instrumentRegister.setCycleDw(eNumber);
                    pointCheckPlanService.save(instrumentRegister);
                    System.out.println();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ResultBody.success("成功");

    }

    public String getNumber(String input, Integer number) {
        if (StringUtils.isEmpty(input)) {
            return "";
        }
        Pattern pattern = Pattern.compile("(\\d+)(\\p{Alpha}+)");
        Matcher matcher = pattern.matcher(input);
        if (matcher.matches()) {
            return matcher.group(number);
        }
        return "";

    }

}
